/**
 *	QuebraLink - Quebrador de links que tem protetor
 *  Copyright (C) 2011  Ranis <ranishot@gmail.com>
 * 
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 *  Classe TrayQL
 *
 *	Esta classe tem a finalidade de criar um incone na area de notificação
 *      do sistema operacional para facilitar a manipulação do programa.
 *
 *	@package QuebraLink
 *	@name QuebraLink.java
 *	@author	Ranishot <ranishot@gmail.com>
 *	@copyright (C)2010 - 2011 Ranishot.
 *	@contributors Rodrigo Borges <rodrigo@rbmsoft.com.br>
 *	@license GNU GPL v3
 *	@version 3.5
 *	@since 14/06/2011
 */
package quebralink;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Set;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import sun.jvmstat.monitor.HostIdentifier;
import sun.jvmstat.monitor.MonitorException;
import sun.jvmstat.monitor.MonitoredHost;
import sun.jvmstat.monitor.MonitoredVm;
import sun.jvmstat.monitor.MonitoredVmUtil;
import sun.jvmstat.monitor.VmIdentifier;

/**
 *
 * @author ranishot
 */
public class TrayQL {

    private GUIQuebraLink gql = new GUIQuebraLink();
    private boolean isOpen = false;
    private boolean isOpenSobre = false;
    private JFrame frame;
    private MenuItem openAndHide;
    private MenuItem sobre;
    private MenuItem exit;
    GUIQuebraLink.SobreGUI sobreJanela = gql.new SobreGUI();

    void CreateTray() {


        final TrayIcon trayIcon;

        gql.initGUI();
        frame = gql.getFrame();

        if (SystemTray.isSupported()) {

            System.out.println("System tray is supported {" + SystemTray.isSupported() + "}");
            System.out.println("Creating system tray..");
            SystemTray tray = SystemTray.getSystemTray();
            System.out.println("Loading tray icon..");
            Image image = new ImageIcon(getClass().getResource("tray.png")).getImage();
            
            System.out.println("Loaded tray icon");

            System.out.println("Loading tray listeners..");
            
            PopupMenu popup = new PopupMenu();
            openAndHide = new MenuItem("Esconder");
            openAndHide.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    if (e.getActionCommand().equalsIgnoreCase("esconder")) {
                        gql.changeGUI();
                        openAndHide.setLabel("Mostrar");
                    } else {
                        gql.changeGUI();
                        frame = gql.getFrame();
                        openAndHide.setLabel("Esconder");
                    }
                }
            });
            frame.addWindowListener(new WindowAdapter() {

                @Override
                public void windowClosing(WindowEvent e) {
                    openAndHide.setLabel("Mostrar");
                }
            });

            sobre = new MenuItem("sobre");
            sobre.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    if (!isOpenSobre) {
                        isOpenSobre = true;
                        sobreJanela.actionPerformed(null);
                        isOpenSobre = false;
                    }
                }
            });

            exit = new MenuItem("Exit");
            exit.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    gql.closeApp();
                }
            });
            popup.add(openAndHide);
            popup.add(sobre);
            popup.addSeparator();
            popup.add(exit);
            RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();

            trayIcon = new TrayIcon(image, "Quebra Link " + GUIQuebraLink.VERSAO, popup);

            ActionListener actionListener = new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    if (!gql.frameIsVisible()) {
                        gql.changeGUI();
                        frame = gql.getFrame();
                        openAndHide.setLabel("Esconder");
                        return;
                    }
                    if (gql.frameIsVisible()) {
                        gql.changeGUI();
                        openAndHide.setLabel("Mostrar");
                    }
                }
            };
            
            System.out.println("Loaded tray listeners");

            trayIcon.setImageAutoSize(true);
            trayIcon.addActionListener(actionListener);

            try {
                tray.add(trayIcon);
                System.out.println("created tray");
            } catch (AWTException e) {
                System.err.println("TrayIcon could not be added.");
            }

        } else {
// System Tray is not supported 
        }
    }
}